home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Demo / tkinter / matt / radiobutton-simple.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.7 KB  |  63 lines

  1. from Tkinter import *
  2.  
  3. # This is a demo program that shows how to 
  4. # create radio buttons and how to get other widgets to 
  5. # share the information in a radio button. 
  6. # There are other ways of doing this too, but 
  7. # the "variable" option of radiobuttons seems to be the easiest.
  8. #
  9. # note how each button has a value it sets the variable to as it gets hit.
  10.  
  11.  
  12. class Test(Frame):
  13.     def printit(self):
  14.     print "hi"
  15.  
  16.     def createWidgets(self):
  17.  
  18.     self.flavor = StringVar()
  19.     self.flavor.set("chocolate")
  20.  
  21.     self.radioframe = Frame(self)
  22.     self.radioframe.pack()
  23.  
  24.     # 'text' is the label
  25.     # 'variable' is the name of the variable that all these radio buttons share
  26.     # 'value' is the value this variable takes on when the radio button is selected
  27.     # 'anchor' makes the text appear left justified (default is centered. ick)
  28.     self.radioframe.choc = Radiobutton(
  29.         self.radioframe, text="Chocolate Flavor", 
  30.         variable=self.flavor, value="chocolate",
  31.         anchor=W)
  32.     self.radioframe.choc.pack(fill=X)
  33.  
  34.     self.radioframe.straw = Radiobutton(
  35.         self.radioframe, text="Strawberry Flavor", 
  36.         variable=self.flavor, value="strawberry",
  37.         anchor=W)
  38.     self.radioframe.straw.pack(fill=X)
  39.  
  40.     self.radioframe.lemon = Radiobutton(
  41.         self.radioframe, text="Lemon Flavor", 
  42.         variable=self.flavor, value="lemon", 
  43.         anchor=W)
  44.     self.radioframe.lemon.pack(fill=X)
  45.     
  46.     # this is a text entry that lets you type in the name of a flavor too.
  47.     self.entry = Entry(self, textvariable=self.flavor)
  48.     self.entry.pack(fill=X)
  49.     self.QUIT = Button(self, text='QUIT', foreground='red',
  50.                command=self.quit)
  51.     self.QUIT.pack(side=BOTTOM, fill=BOTH)
  52.  
  53.  
  54.     def __init__(self, master=None):
  55.     Frame.__init__(self, master)
  56.     Pack.config(self)
  57.     self.createWidgets()
  58.  
  59. test = Test()
  60.  
  61. test.mainloop()
  62.